package com.artron.mediaartron.ui.fragment.center;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.artron.baselib.base.BaseLoadingFragment;
import com.artron.baselib.base.BaseStaticFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.adapter.OrderFragmentPagerAdapter;

import butterknife.BindView;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-02.
 */

public class MyOrdersFragment extends BaseStaticFragment {

    @BindView(R.id.MyOrdersFragment_tab)
    protected TabLayout mTabLayout;
    @BindView(R.id.MyOrdersFragment_pager)
    protected ViewPager mViewPager;
    private int mPosition;
    private OrderFragmentPagerAdapter mFragmentAdapter;

    public static MyOrdersFragment newInstance(int position) {
        MyOrdersFragment fragment = new MyOrdersFragment();
        fragment.init(position);
        return fragment;
    }

    private void init(int position) {
        this.mPosition = position;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_orders;
    }

    @Override
    protected void initView() {
        mFragmentAdapter = new OrderFragmentPagerAdapter(
                getChildFragmentManager());
        mViewPager.setAdapter(mFragmentAdapter);

        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                Fragment fragment = mFragmentAdapter.getFragment(position);
                if (fragment instanceof BaseLoadingFragment) {
                    ((BaseLoadingFragment) fragment).show();
                }
            }
        });

        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.post(new Runnable() {
            @Override
            public void run() {
                mViewPager.setCurrentItem(mPosition);
            }
        });
    }

    public void refreshFragment() {
        for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
            Fragment fragment = mFragmentAdapter.getFragment(i);
            if (fragment instanceof MyOrderContentFragment) {
                ((MyOrderContentFragment) fragment).showImmediate();
            }
        }
    }
}
